import requests
import datetime,time
# import os
import pywifi
# import subprocess
from pywifi import const,Profile
# import netifaces
def send(id,password):
  url1 = "http://172.16.2.9:8080/eportal/InterFace.do?method=login&userId="+id+"&password="+password+"&queryString=wlanuserip%3D4728b530577b6dbb84243d87d33c20d9%26wlanacname%3Db3b3dd674434a9db70df973004a20ca6%26ssid%3D%26nasip%3D7b3ad2fa097d34acd2131b2fd77ea6f0%26snmpagentip%3D%26mac%3D93489a207779817531aaf8db14aa1476%26t%3Dwireless-v2%26url%3D709db9dc9ce334aa02a9e1ee58ba6fcf3bc3349e947ead368bdd021b808fdbac30c65edaa96b0727%26apmac%3D%26nasid%3Db3b3dd674434a9db70df973004a20ca6%26vid%3Dce3558e0b2a8a823%26port%3D9bdeca5efa34f87c%26nasportid%3Df5eb983692924fa26e6431fe9df4835f02c7eedb2507238a361fd6feccf083ac7d930a603cb25e71&passwordEncrypt=false"
  current_time = datetime.datetime.now()
  print("当前时间为:" + str(current_time))
  flag=0
  while True:
    try:
      flag+=1
      if flag>5:
        print("尝试次数过多，请检查网络连接")
        break
      # print("连接本人账号")
      print("当前账号为",id)
      res = requests.post(url1)
      if(res.json()['result']=='success'):
        print("登录成功")
        break
      else:
        print("登录失败,请查看错误代码")
        print(res.json()['text'])
        input("请按任意键继续")
    except Exception as e:
      print("出问题了！连接错误···")
      print(e)
      time.sleep(5)
  print("倒计时:3")
  time.sleep(1)
  print("倒计时:2")
  time.sleep(1)
  print("倒计时:1")
  time.sleep(1)
  return

userid = "xxxxxxxxxx" #账号(学号)
password = "xxxxxx" #密码(身份证后六位)
# send(userid,password)

wifi = pywifi.PyWiFi()#获取网卡接口
ifaces = wifi.interfaces()[0]#得到第一个网卡
wifistatus = ifaces.status()#获取wifi连接状态

if wifistatus == const.IFACE_DISCONNECTED:#检查wifi是否断开
  print("检测到wifi断开,正在连接中···")
  profile: Profile = pywifi.Profile()#wifi断开的操作
  profile.ssid = "TKY-WIFI"#设置wifi名称
  profile.key = "CIPHER_TYPE_NONE"#设置wifi密码
  # ifaces.remove_all_network_profiles()#删除已连接的所有wifi文件
  tep_profile = ifaces.add_network_profile(profile)#加载新的wifi连接文件
  ifaces.connect(tep_profile)  #连接上面的wifi文件
  time.sleep(2)
  if ifaces.status() == const.IFACE_CONNECTED:#如果wifi已连接
    print("连接wifi成功,正在发送登录请求···")
    send(userid,password)#发送请求
  else:# 如果仍未连接
    print("出问题了!wifi未连接成功···")
    time.sleep(2)
else:
  #wifi没断开的操作
  print("已连接上wifi,正在发送登录请求···")
  send(userid,password)#发送请求


